//
//  SearchViewController.m
//  HappyToday
//
//  Created by Ale (ziming) Lee on 16/8/5.
//  Copyright © 2016年 ziming_lee. All rights reserved.
//

#import "SearchViewController.h"
#import "HeaderView.h"
#import "SearchModel.h"
#import "SearchTableViewCell.h"

@interface SearchViewController ()<UITableViewDataSource,UITableViewDelegate,PullTableViewDelegate,UIScrollViewDelegate>{

    UIScrollView *_pageScrollView;
    UIPageControl *_pageControl;
    NSMutableArray *_dataList;

}

@property (weak, nonatomic) IBOutlet PullTableView *searchTableView;
@end

@implementation SearchViewController

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {

        self.tabBarItem.image = [UIImage imageNamed:@"icon_tab_explore_normal@2x"];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];


    [self _createSearchTableView];
    [self _loadData];

}

-(void)_loadData{
    NSArray *rootDataArray = [DataParser jsonObjectFromFileName:@"SearchJSONDirections"];
    _dataList = [[NSMutableArray alloc]initWithCapacity:1];
    for (NSDictionary *dic in rootDataArray) {

        SearchModel *searchModel = [[SearchModel alloc]initWithDic:dic];

        [_dataList addObject:searchModel];

    }

}
-(void)_createSearchTableView{

    _searchTableView.dataSource = self;
    _searchTableView.delegate = self;
    _searchTableView.pullDelegate = self;


    HeaderView *headerView = [[[NSBundle mainBundle]loadNibNamed:@"HeaderView" owner:nil options:nil] lastObject];


    NSArray *pageControlArray = [[NSBundle mainBundle]loadNibNamed:@"PageControl" owner:nil options:nil];


    UIView *pageView1 = [pageControlArray firstObject];
    UIView *pageView2 = [pageControlArray lastObject];
    pageView2.frame = CGRectMake(pageView1.size.width, 0, pageView1.width, pageView1.height);

    _pageScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, KScreenW, pageView1.height)];
    _pageScrollView.contentSize = CGSizeMake(pageView1.width *2,pageView1.height);
    _pageScrollView.pagingEnabled = YES;
    _pageScrollView.showsVerticalScrollIndicator = YES;
    _pageScrollView.delegate = self;

    [_pageScrollView addSubview:pageView1];
    [_pageScrollView addSubview:pageView2];
    [headerView addSubview:_pageScrollView];

    //创建pageControl
    _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, _pageScrollView.bottom, KScreenW, 20)];
    [headerView addSubview:_pageControl];
    _pageControl.numberOfPages = 2;
    _pageControl.currentPage = 0;
    _pageControl.backgroundColor = [UIColor grayColor];


    _searchTableView.tableHeaderView = headerView;
    

}

#pragma ----mark------scrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

//    CGFloat offsetx = _pageScrollView.contentOffset.x;
//    NSLog(@"%.2f",offsetx);

}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    CGFloat offsetx = _pageScrollView.contentOffset.x;
    _pageControl.currentPage = (NSInteger)offsetx/_pageScrollView.width;
    NSLog(@"%d",_pageControl.currentPage);
}
#pragma mark---------tableViewDelegate---------------------
-(void)pullTableViewDidTriggerLoadMore:(PullTableView *)pullTableView{

    NSLog(@"上啦加载");
}
-(void)pullTableViewDidTriggerRefresh:(PullTableView *)pullTableView{

    NSLog(@"下拉刷新");

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return _dataList.count;

}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


    SearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"search_cell"];
    if (!cell) {

        cell = [[[NSBundle mainBundle]loadNibNamed:@"SearchTableViewCell" owner:nil options:nil] lastObject];
    }

    SearchModel *searchModel = [_dataList objectAtIndex:indexPath.row];
    cell.searchModel = searchModel;
    
    return cell;

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{


    return 104;
}
#pragma mark---------tableViewDelegate---------------------


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
